﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using scheduleMVC.Models;
using scheduleMVC.DAL;
using System.Web.Security;

namespace scheduleMVC.Controllers
{
    public class EmployeeController : Controller
    {
        private EmployeeContext db = new EmployeeContext();

        private string userID { get; set; }

        // GET: /Employee/
        public ActionResult Index()
        {
            
            if(User.Identity.IsAuthenticated)
            return View(db.Employees.ToList());
            else
            {
                return View("EmployeeLogin");
            }
        }

        // GET: /Employee/Details/5
        public ActionResult Details(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EmployeeModel employeemodel = db.Employees.Find(id);
            if (employeemodel == null)
            {
                return HttpNotFound();
            }
            return View(employeemodel);
        }




       







        // GET: /Employee/Create
        public ActionResult Create(string id,string username)
        {
            IEnumerable<SelectListItem> User;
            User = new[]
                  {
                    new SelectListItem { Value = id, Text = username },
                  };
            ViewBag.User = User;
            return View();
        }

        // POST: /Employee/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Employee_ID,FirstName,LastName,Salary_Type,Manager_ID,Title,Hire_Date,Salary,Vacation_Hrs,SickLeave_Hrs,UserId")] EmployeeModel employeemodel)
        {


            //employeemodel.UserId = ViewBag.userID;
            //Set the creation and modified date to Today date and time
            employeemodel.Created_Date = DateTime.Now;
            employeemodel.Modified_Date = DateTime.Now;
            employeemodel.Hire_Date = DateTime.Now;
            if (ModelState.IsValid)
            {
                

                db.Employees.Add(employeemodel);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(employeemodel);
        }

        // GET: /Employee/Edit/5
        public ActionResult Edit(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EmployeeModel employeemodel = db.Employees.Find(id);
            if (employeemodel == null)
            {
                return HttpNotFound();
            }
            return View(employeemodel);
        }

        // POST: /Employee/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="Employee_ID,FirstName,LastName,Salary_Type,Manager_ID,Title,Hire_Date,Salary,Vacation_Hrs,SickLeave_Hrs,UserId")] EmployeeModel employeemodel)
        {
            if (ModelState.IsValid)
            {
                employeemodel.Modified_Date = DateTime.Now;
                db.Entry(employeemodel).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(employeemodel);
        }

        // GET: /Employee/Delete/5
        public ActionResult Delete(int id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EmployeeModel employeemodel = db.Employees.Find(id);
            if (employeemodel == null)
            {
                return HttpNotFound();
            }
            return View(employeemodel);
        }

        // POST: /Employee/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            EmployeeModel employeemodel = db.Employees.Find(id);
            db.Employees.Remove(employeemodel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
